package verify

import (
	models "HeadLineNews/proto_models/manage/admin_model"
	"github.com/go-playground/validator/v10"
	"time"
)

const (
	dataTimeFormat = "2006-01-02 15:04:05"
)

// registerAdminRequestValidation 管理员注册 eqfield
func registerAdminRequestValidation() {
	validate.RegisterStructValidation(func(sl validator.StructLevel) {
		if s, ok := sl.Current().Interface().(models.RequestAdminRegister); ok {
			if s.AdminPwd != s.RepPwd {
				sl.ReportError(
					nil,
					"repPwd",
					"",
					"eqfield",
					"adminPwd",
				)
			}
		}

	}, models.RequestAdminRegister{})
}

// registerListAdminValidation 展示管理员
func registerListAdminValidation() {
	validate.RegisterStructValidation(func(sl validator.StructLevel) {
		if s, ok := sl.Current().Interface().(models.RequestListAdmin); ok {
			// 起始时间在结束时间之后
			rangTimeStart, _ := time.Parse(dataTimeFormat, s.TimeRange.TimeRangeStart)
			rangTimeEnd, _ := time.Parse(dataTimeFormat, s.TimeRange.TimeRangeEnd)
			if rangTimeStart.After(rangTimeEnd) {
				sl.ReportError(nil,
					"timeRangeStart",
					"",
					"gtefield",
					"timeRangeEnd",
				)
			}
		}
	}, models.RequestListAdmin{})
}